package org.pub.lunch.mashup.google;

import java.io.Serializable;
import java.net.URL;
import java.util.ArrayList;

import org.pub.lunch.mashup.google.js.JSArray;
import org.pub.lunch.mashup.google.js.JSObject;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.location.Location;

/**
 * @author Kyungseop Kim
 * 
 */
public class Restaurant implements Serializable, Comparable<Restaurant> {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8608367384977112177L;

	private String name;
	private Location location;
	private String address;
	private String phone;
	private URL photoUrl;
	private Drawable drawable;
	private ArrayList<String> categories;
	
	//add austin
	private int x;
	private int y;

	
	public Drawable getDrawable() {
		return drawable;
	}

	public void setDrawable(Drawable drawable) {
		this.drawable = drawable;
	}
	
	public ArrayList<String> getCategories() {
		return categories;
	}

	public void setCategories(ArrayList<String> cat) {
		this.categories = cat;
	}

	public URL getPhotoUrl() {
		return photoUrl;
	}

	public void setPhotoUrl(URL photoUrl) {
		this.photoUrl = photoUrl;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Location getLocation() {
		return location;
	}

	public void setLocation(Location location) {
		this.location = location;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}
	
	public Bitmap getBitmap(){
		if( drawable == null )
			return null;
		return ((BitmapDrawable)drawable).getBitmap();
	}
	
	

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	public Restaurant(String name, String address, String phone,
			Location location) {
		super();
		this.name = name;
		this.location = location;
		this.address = address;
		this.phone = phone;
	}

	public Restaurant(JSObject object) throws Exception {
		// TODO Auto-generated constructor stub
		this.setName(object.getString("name"));
		JSObject loc = object.getObject("latlng");
		this.setLocation(new Location("current point"));
		location.setLatitude(Double.valueOf(loc.getString("lat")));
		location.setLongitude(Double.valueOf(loc.getString("lng")));
		JSObject info = object.getObject("infoWindow");
		this.setAddress(info.getArray("addressLines").getString(0));
		this.setPhone(info.getArray("phones").getObject(0).getString("number"));
		String photo = null;
		if(info.hasKey("photoUrl") && (photo=info.getString("photoUrl"))!=null)
			this.setPhotoUrl(new URL(photo));

		if(info.hasKey("cat2")) {
			categories = new ArrayList<String>();
			
			JSArray arr = info.getArray("cat2");
			for(int i=0;i<arr.length();i++) {
				JSObject obj = arr.getObject(i);
				String name = obj.getString("name");
				if(name.equals("\uC74C\uC2DD\uC810"))
					continue;
				categories.add(name);
			}
		}
	}

//	@Override
	public int compareTo(Restaurant another) {
		//이름순 정렬
		return this.getName().compareTo(another.getName());
	}

}
